/**
 * 				ClientSystray.java
 *				------------
 *		
 *   Author:	bin04017
 *   ID:		bin04017
 *	 Created:	24.01.2007
 *	 Project:	Mochito
 *	 Package:	tools.systray
 *	 Usage:		
 */
package mochito.tools.clientPackage.systray;
import java.util.Vector;

import snoozesoft.systray4j.SubMenu;
import snoozesoft.systray4j.SysTrayMenu;
import snoozesoft.systray4j.SysTrayMenuEvent;
import snoozesoft.systray4j.SysTrayMenuIcon;
import snoozesoft.systray4j.SysTrayMenuItem;
import snoozesoft.systray4j.SysTrayMenuListener;

/**
 * 				Systray.java
 *				------------
 *		
 *   Author:	Moser Dominik
 *   ID:		bin04017
 *	 Created:	24.01.2007
 *	 Project:	MochitoClipboard
 *	 Package:	
 *	 Usage:		
 */

/**
 * @author bin04017
 *
 */
public class ClientSystray implements SysTrayMenuListener{

	private SysTrayMenu menu;
	
	private SysTrayMenuItem subItem1 = null;
       
	private SysTrayMenuItem subItem2 = null;
	
	private Boolean running = false;
	
	private boolean single = false;
	
	private boolean ctrlC = false;
	
	private static final String[] toolTips =
    {
        "Mochito Client stoped",
        "Mochito Client running!"
    };
	
	static final SysTrayMenuIcon[] icons =
    {
        // the extension can be omitted
        new SysTrayMenuIcon( "icons/client_off" ),
        new SysTrayMenuIcon( "icons/client_on" )
    };

	private int currentIndexIcon = 0;

	private int currentIndexTooltip = 0;
	
	public ClientSystray(){
		icons[0].addSysTrayMenuListener( this );
		icons[1].addSysTrayMenuListener( this );
	}
	public void createMenu()
    {
		subItem1 = new SysTrayMenuItem( "Start", "start" );
        subItem1.addSysTrayMenuListener( this );
        subItem1.setEnabled( true );

        subItem2 = new SysTrayMenuItem( "Stop", "stop" );
        subItem2.addSysTrayMenuListener( this );
        subItem2.setEnabled(false);
                           
        Vector<SysTrayMenuItem> items = new Vector<SysTrayMenuItem>();
        items.add( subItem1 );
        items.add( subItem2 );
        
        // create a submenu and insert the previously created items
        SubMenu subMenu = new SubMenu( "Mochito", items );

        SysTrayMenuItem itemInfo = new SysTrayMenuItem( "Client", "client" );
        itemInfo.addSysTrayMenuListener( this );
        
        SysTrayMenuItem itemSingle = new SysTrayMenuItem( "Single", "single" );
        itemSingle.addSysTrayMenuListener( this );
        
        SysTrayMenuItem itemExit = new SysTrayMenuItem( "Exit", "exit" );
        itemExit.addSysTrayMenuListener( this );

        SysTrayMenuItem itemCTRL = new SysTrayMenuItem("CTRL-C","ctrl-c");
        itemCTRL.addSysTrayMenuListener(this);
        
        // create the main menu
        menu = new SysTrayMenu( icons[0], toolTips[0]);

        // insert items
        menu.addItem( itemExit );
        menu.addSeparator();
        menu.addItem( subMenu );
        menu.addSeparator();
        menu.addItem(itemCTRL);
        menu.addItem(itemSingle);
        menu.addSeparator();
        menu.addItem(itemInfo);
        
    }
	public void iconLeftClicked(SysTrayMenuEvent arg0) {
		//TODO make
	}
	public void iconLeftDoubleClicked(SysTrayMenuEvent arg0) {
		starting();
	}
	
	public void menuItemSelected(SysTrayMenuEvent arg0) {
		if( arg0.getActionCommand().equals( "exit" ) ) System.exit( 0 );
		else if(arg0.getActionCommand().equals("start")) starting();
		else if(arg0.getActionCommand().equals("stop")) stoping();
		else if(arg0.getActionCommand().equals("single")) single();
		else if(arg0.getActionCommand().equals("ctrl-c")) ctrlc();
		
	}
	private void single() {
		setSingle(true);
	}
	private void ctrlc() {
		ctrlC = !ctrlC;
	}
	private void starting() {
		running = true;
		setIcons();
		setItemsStatus();
	}
	private void stoping() {
		running = false;	
		setIcons();
		setItemsStatus();
	}
	
	private void setIcons(){
		if( currentIndexIcon   == 0 ) currentIndexIcon = 1;
        else currentIndexIcon = 0;
		menu.setIcon( icons[ currentIndexIcon ] );
		if( currentIndexTooltip   == 0 ) currentIndexTooltip = 1;
        else currentIndexTooltip = 0;
        menu.setToolTip( toolTips[ currentIndexTooltip ] );
	}
	private void setItemsStatus(){
		subItem1.setEnabled(!running);
		subItem2.setEnabled(running);
	}
	/**
	 * @return the icon
	 */
	public SysTrayMenuIcon getIcon() {
		return icons[currentIndexIcon];
	}
	/**
	 * @return the toolTip
	 */
	public String getToolTip() {
		return toolTips[currentIndexTooltip];
	}
	/**
	 * @return the running
	 */
	public Boolean getRunning() {
		return running;
	}
	/**
	 * @return the single
	 */
	public boolean isSingle() {
		return single;
	}
	/**
	 * @param single the single to set
	 */
	public void setSingle(boolean single) {
		this.single = single;
	}
	public boolean isCTRLc() {
		// TODO Auto-generated method stub
		return ctrlC;
	}
}
